/**************************************************************************************************
 * @file        os_new.h
 * @brief       跨平台自定义内存分配操作封装，使用tlsf 封装，解决内存碎片
 * @details     提供带文件和行号追踪功能的自定义new操作符，用于内存泄漏检测和调试
 *
 * @author      taoist.luo
 * @version     1.0.0
 * @date        2024-09
 *
 * Copyright (c) 2024 taoist.luo (https://gitee.com/harmonyluo(https://blog.csdn.net/harmonyluo))
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *************************************************************************************************/

#pragma once

#include <cstdlib>
#include <memory>
#include <new>

#include "memery/os_memery_manager.h"

#define USE_OS_MEMERY_MANAGER 1  // 是否启用自定义的内存管理（使用os_new 申请内存时候可以使用内存泄漏检测工具）
#define REPLACE_SYS_NEW       1  // 是否完全重载系统的内存分配（使用os_new 或者标准的new 的时候使用内存泄漏检测工具）

#if USE_OS_MEMERY_MANAGER
    #if REPLACE_SYS_NEW
inline void* operator new(size_t size)
{
    void* ptr = os_memery_manager_t::get_instance()->allocate(size, "unkown", 0);
    if (!ptr) {
        throw std::bad_alloc();
    }
    return ptr;
}
inline void* operator new[](size_t size)
{
    return operator new(size);
}
inline void* operator new(size_t size, const std::nothrow_t&) noexcept
{
    return os_memery_manager_t::get_instance()->allocate(size, "unkown", 0);
}
inline void* operator new[](size_t size, const std::nothrow_t&) noexcept
{
    return operator new(size, std::nothrow);
}
    #endif
inline void* operator new(size_t size, const char* file, int line)
{
    void* ptr = os_memery_manager_t::get_instance()->allocate(size, file, line);
    if (!ptr) {
        throw std::bad_alloc();
    }
    return ptr;
}
inline void operator delete(void* ptr) noexcept
{
    os_memery_manager_t::get_instance()->deallocate(ptr);
}

inline void* operator new[](size_t size, const char* file, int line)
{
    return operator new(size, file, line);
}

inline void operator delete[](void* ptr) noexcept
{
    operator delete(ptr);
}

inline void operator delete(void* ptr, size_t) noexcept
{
    operator delete(ptr);
}

inline void operator delete[](void* ptr, size_t) noexcept
{
    operator delete(ptr);
}

inline void* operator new(size_t size, const std::nothrow_t&, const char* file, int line) noexcept
{
    return os_memery_manager_t::get_instance()->allocate(size, file, line);
}

inline void* operator new[](size_t size, const std::nothrow_t&, const char* file, int line) noexcept
{
    return operator new(size, std::nothrow, file, line);
}
    #if __cplusplus >= 201703L
// C++17 大小的 delete 操作符
void operator delete(void* ptr, std::align_val_t) noexcept
{
    operator delete(ptr);
}

void operator delete[](void* ptr, std::align_val_t) noexcept
{
    operator delete(ptr);
}

void operator delete(void* ptr, size_t, std::align_val_t) noexcept
{
    operator delete(ptr);
}

void operator delete[](void* ptr, size_t, std::align_val_t) noexcept
{
    operator delete(ptr);
}
    #endif
#endif
#define os_new_super(type, ...) new (std::nothrow, __FILE__, __LINE__) type(__VA_ARGS__)
#define os_new                  new (std::nothrow, __FILE__, __LINE__)
#define os_delete               delete